﻿using System;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.Utilities;
using StructureMap;
using MedicalCare.Messages;

namespace MedicalCare.Admin
{
    public partial class UserList : System.Web.UI.Page
    {
        private const string OrderKey = "CreateDT";
        private const string OrderValue = "DESC";
        private const int PageSize = 10;
        private string _whereString = "1=1";
        private IUserAdaptor _userAdaptor;
        //Champion 2013/1/4
        private int userId { get { return Convert.ToInt32(ViewState["userId"]); } set { ViewState["userId"] = value; } }
        private string action { get { return Convert.ToString(ViewState["action"]); } set { ViewState["action"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _userAdaptor = ObjectFactory.GetInstance<IUserAdaptor>();
                hideOrderby.Value = OrderKey; // 0514
                userListPager.AlwaysShow = true;
                userListPager.PageSize = PageSize;
                userListPager.RecordCount = _userAdaptor.GetRecordsTotal(_whereString);
                BindDataList(true);
            }
        }

        private void BindDataList(bool rebindPager)
        {
            var currentPageSize = 1;
            try
            {
                currentPageSize = Request.QueryString["Page"] != null ? Convert.ToInt32(Request.QueryString["Page"]) : userListPager.CurrentPageIndex;
            }
            catch
            {
                currentPageSize = 1;
            }

            var userAdaptor = ObjectFactory.GetInstance<IUserAdaptor>();
            //Champion 2013/1/4
            _whereString = "Valid!='D'";
            var source = userAdaptor.GetPagingRecords(OrderKey, OrderValue, currentPageSize, PageSize, _whereString);

            var objPds = new PagedDataSource {DataSource = source, AllowPaging = true, PageSize = PageSize};
            UsersList.DataSource = objPds;
            UsersList.DataBind();
        }

        protected void msgPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            userListPager.CurrentPageIndex = e.NewPageIndex;
            BindDataList(true);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Admin/UserDetail.aspx?action=" + ActionType.NEW.ToString());
        }

        //Champion 2013/1/4
        protected void UsersList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            _userAdaptor = ObjectFactory.GetInstance<IUserAdaptor>();
            try
            {
                userId = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName.ToLower())
                {
                    case "del":
                        UserMessage userMessage =getUserMessage(ActionType.DELETE.ToString());
                        int result = _userAdaptor.DeleteUserById(userMessage);
                        BindDataList(true);
                        break;
                    case "edit":
                        Server.Transfer("~/Admin/UserDetail.aspx?action=" + ActionType.EDIT.ToString() + "&Id=" + userId.ToString());
                        break;
                }
            }
            catch
            {

            }
        }

        //Champion 2013/1/3
        private UserMessage getUserMessage(string action)
        {
            UserMessage userMessage = null;
            if (ActionType.DELETE.ToString().Equals(action))
            {
                userMessage = new UserMessage()
                {
                    Id = userId,
                    ModifyUser = AppPage.CurrentUser.UserName,
                    Valid = "D"
                };
            }
            return userMessage;
        }
    }
}